package com.xiaodou.mjf.application.user.manager;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import com.xiaodou.mjf.application.basic.utils.SequenceGenerator;
import com.xiaodou.mjf.application.core.service.AccountService;
import com.xiaodou.mjf.application.core.service.BankCardCashioService;
import com.xiaodou.mjf.application.core.service.BankCardService;
import com.xiaodou.mjf.application.core.service.DepositService;
import com.xiaodou.mjf.application.core.service.NotesBookService;
import com.xiaodou.mjf.application.core.service.WithdrawalService;
import com.xiaodou.mjf.application.user.manager.result.RegisterResult;
import com.xiaodou.mjf.application.user.service.MemberProfileService;
import com.xiaodou.mjf.application.user.service.MemberSecurityService;
import com.xiaodou.mjf.application.user.service.MemberService;
import com.xiaodou.mjf.entity.core.Account;
import com.xiaodou.mjf.entity.core.BankCard;
import com.xiaodou.mjf.entity.core.BankCardCashio;
import com.xiaodou.mjf.entity.core.Deposit;
import com.xiaodou.mjf.entity.core.NotesBook;
import com.xiaodou.mjf.entity.core.Withdrawal;
import com.xiaodou.mjf.entity.user.Member;
import com.xiaodou.mjf.entity.user.MemberProfile;
import com.xiaodou.mjf.entity.user.MemberSecurity;
import com.xiaodou.mjf.infrastructure.common.app.CommonManager;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;
import com.xiaodou.mjf.infrastructure.common.utils.Encrypt;

@Component
@Transactional
public class MemberManager extends CommonManager{

	@Resource MemberService memberService;
	@Resource MemberProfileService memberProfileService;
	@Resource MemberSecurityService memberSecurityService;
	@Resource AccountService accountService;
	@Resource DepositService depositService;
	@Resource NotesBookService notesBookService;
	@Resource WithdrawalService withdrawalService;
	@Resource BankCardService bankCardService;
	@Resource BankCardCashioService bankCardCashioService;
	//存款账户
	public static final String ACCOUNT_TYPE0 = "01";
	
	//贷款账户
	public static final String ACCOUNT_TYPE1 = "02";
	
	/**
	 * 用户注册
	 * @param mobile
	 * @param code
	 * @param password
	 * @return
	 */
	public RegisterResult register(String mobile,String password){
		
		//需要开户：account
		RegisterResult registerResult =  new RegisterResult(result(false));
		try{
						
			Member member = new Member();
			MemberProfile profile = new MemberProfile();
			MemberSecurity security = new MemberSecurity();		
			Account account = new Account();
			//设置当前时间
			member.setLastLoginTime(DateUtils.getCurrentTime());
			//设置登陆次数
			member.setLoginCount(1);
			//新增member表
			memberService.save(member);
			// 新增的id与security表关联  member.id=security.id
			security.setId(member.getId());
			security.setMobile(mobile);
			security.setPassword(Encrypt.entryptPassword(password));
			//新增的id与Profile表关联 member.id=profile.id
			profile.setId(member.getId());
			memberProfileService.save(profile);
			memberSecurityService.save(security);
			//新增的id与Profile表关联 member.id=account.id
			Double zero = 0D;
			//存款账户
			String number = SequenceGenerator.generateAccountNumber(SequenceGenerator.TYPE_DEFAULT_ACCOUNT);
			account.setNumber(number);
			account.setMemberId(member.getId());
			account.setBookBalance(zero);
			account.setAvaliableBalance(zero);
			account.setPreviousBalance(zero);
			account.setHoldAmount(zero);
			account.setUnclearAmount(zero);
			account.setLastCreditAmount(zero);
			account.setInterestAdjAmount(zero);
			account.setStatus(0);
			account.setVoucherBalance(0D);
			//初始值  
			account.setCreator(member.getId());
			//初始值  
			account.setModifier(member.getId());
			account.setTotalProfit(0D);
			accountService.save(account);
			
			registerResult.setSuccess(true);
			registerResult.setUid(member.getId());			
			
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

		return registerResult;
	}
	
	/**
	 * 多表事务修改 提现
	 * @param FinancingStatistics
	 * @param invest
	 */
	public void withdraw(Withdrawal withdrawal,Account account,NotesBook notesBook){
		
		try{
			withdrawalService.save(withdrawal);
			accountService.save(account);
			//notesBookService.save(notesBook);
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

	}
	/**
	 * 多表事务修改 充值
	 * @param FinancingStatistics
	 * @param invest
	 */
	public void deposit(Deposit deposit,Account account,NotesBook notesBook){
		
		try{
			depositService.save(deposit);
			accountService.save(account);
			notesBookService.save(notesBook);
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

	}
	/**
	 * 多表事务修改 身份认证
	 * @param FinancingStatistics
	 * @param invest
	 */
	public void identityAuth(Account account, Member member){
		
		try{			
			accountService.save(account);
			memberService.save(member);
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

	}
	/**
	 * 多表事务修改 银行卡添加
	 * @param FinancingStatistics
	 * @param invest
	 */
	public void bankcardAdd(BankCard bankCard, BankCardCashio bankCardCashio){
		
		try{			
			bankCardService.save(bankCard);
			bankCardCashioService.save(bankCardCashio);
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

	}
	/**
	 * 多表事务修改 银行卡删除
	 * @param FinancingStatistics
	 * @param invest
	 */
	public void bankcardDel(BankCard bankCard, BankCardCashio bankCardCashio){
		
		try{			
			bankCardService.save(bankCard);
			bankCardCashioService.save(bankCardCashio);
		}catch(Exception e){
			logger.error(e.toString());
			throw e;
		}

	}
}
